const {
  createOrder,
  findAllOrder,
  updateOrder,
} = require("../service/orderService");
const { internalServerError } = require("../constant/error");
class OrderController {
  async create(ctx) {
    try {
      const user_id = ctx.state.user.id;
      const { address_id, goods_info, total } = ctx.request.body;
      const order_code = "LY" + Date.now();
      const res = await createOrder({
        user_id,
        address_id,
        goods_info,
        total,
        order_code,
        status: "0", // 默认状态
      });
      ctx.body = {
        code: 0,
        msg: "创建成功",
        result: res,
      };
    } catch (error) {
      console.error("失败:", error); // 打印错误信息
      return ctx.app.emit("error", internalServerError, ctx);
    }
  }
  async findAll(ctx) {
    const { pageNum = 1, pageSize = 10, status = 0 } = ctx.request.query;
    const res = await findAllOrder(pageNum, pageSize, status);
    console.log("Response data:", res);
    ctx.body = {
      code: 0,
      msg: "获取订单列表成功",
      result: res,
    };
  }
  async update(ctx) {
    const id = ctx.request.query.id;
    const { status } = ctx.request.body;
    const res = await updateOrder(id, status);
    ctx.body = {
      code: 0,
      msg: "更新订单状态成功",
      result: res,
    };
  }
}
module.exports = new OrderController();
